/*
 * @Author: 陈永浩(cyh1231wp@163.com)
 * @Date: 2020-12-03 13:42:56
 * @LastEditors: 陈永浩(cyh1231wp@163.com)
 * @LastEditTime: 2021-02-23 21:45:48
 */
import router from '@/router'
import store from '@/store'
import {
  recordRoute,
  routesWhiteList,
} from '@/config'
// router.onReady(async () => {
//   console.log('onReady');
//   let hasToken = store.getters['user/accessToken']
//   console.log(hasToken);
//   if (hasToken) {
//     let hasRoutes = store.getters['routes/accessRoutes']
//     if (!hasRoutes) {
//       let accessRoutes = await store.dispatch('routes/setAllRoutes')
//       console.log(accessRoutes);
//       if (accessRoutes) {
//         router.addRoutes(accessRoutes);
//       }
//     }
//   }
// })
router.beforeEach(async (to, from, next) => {
  console.log('beforeEach');
  if (routesWhiteList.indexOf(to.path) !== -1) {
    return next()
  }
  let hasToken = store.getters['user/accessToken']
  // console.log(hasToken);
  if (hasToken) {
    let hasRoutes = store.getters['routes/accessRoutes']
    if (hasRoutes) {
      return next()
    }
    let accessRoutes = await store.dispatch('routes/setAllRoutes')
    router.addRoutes(accessRoutes);

    next({ ...to, replace: true })

  } else {

    if (recordRoute) {
      next(`/login?redirect=${to.path}`)
    } else {
      next('/login')
    }

  }
})
router.afterEach(() => {

})
